package ws;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;


public class Conexionws {

	private static String METHOD_NAME ;
	private static String NAMESPACE ;
	private static String URL;
	String datosJsonString= "";

	public void coneccionwsPropio(){


		NAMESPACE = "http://webservices";
		URL = "http://186.10.199.201:8080/NotasDaem/services/NotasDaemApp?wsdl";
	}

	public void coneccionwsExterno(){


		NAMESPACE = "http://webservices";
		URL = "http://200.13.4.205:8080/NotasDaem/services/NotasDaemApp?wsdl";
	}



	/**
	 * 
	 * @param asignaturatex
	 * @param n1
	 * @param n2
	 * @param n3
	 * @param idpersona
	 * @return
	 */
	public String guardanotas(String asignaturatex, int n1,int n2, int n3, int idpersona){

		coneccionwsExterno();

		METHOD_NAME = "agregaAsignatura";

		String SOAP_ACTION = NAMESPACE + METHOD_NAME;

		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		PropertyInfo asignatura = new PropertyInfo();
		asignatura.setName("asignatura");
		asignatura.setValue(asignaturatex);
		asignatura.setType(String.class);

		PropertyInfo nota1 = new PropertyInfo();
		nota1.setName("nota1");
		nota1.setValue(n1);
		nota1.setType(int.class);
		
		PropertyInfo nota2 = new PropertyInfo();
		nota2.setName("nota2");
		nota2.setValue(n2);
		nota2.setType(int.class);
		
		PropertyInfo nota3 = new PropertyInfo();
		nota3.setName("nota3");
		nota3.setValue(n3);
		nota3.setType(int.class);
		
		PropertyInfo ida = new PropertyInfo();
		ida.setName("idalumno");
		ida.setValue(idpersona);
		ida.setType(int.class);
		
		request.addProperty(asignatura);
		request.addProperty(nota1);
		request.addProperty(nota2);
		request.addProperty(nota3);
		request.addProperty(ida);
		
		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		soapEnvelope.dotNet = true;
		soapEnvelope.implicitTypes =true;
		soapEnvelope.setOutputSoapObject(request);

		AndroidHttpTransport aht = new AndroidHttpTransport(URL);


		try
		{
			aht.call(SOAP_ACTION, soapEnvelope);
			SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
			datosJsonString =resultString.toString();
			return datosJsonString;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			datosJsonString = "Error Conexion";}
		return datosJsonString;


	}
	/**
	 * Metodo que calcula el promedio general de las notas
	 * @param idperso
	 * @return
	 */
	public String pedirPromedio(int idperso){

		coneccionwsExterno();

		METHOD_NAME = "mostrarPromedio";

		String SOAP_ACTION = NAMESPACE + METHOD_NAME;

		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		PropertyInfo ida = new PropertyInfo();
		ida.setName("idPersona");
		ida.setValue(idperso);
		ida.setType(int.class);

		request.addProperty(ida);
		
		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		soapEnvelope.dotNet = true;
		soapEnvelope.implicitTypes =true;
		soapEnvelope.setOutputSoapObject(request);

		AndroidHttpTransport aht = new AndroidHttpTransport(URL);


		try
		{
			aht.call(SOAP_ACTION, soapEnvelope);
			SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
			datosJsonString =resultString.toString();
			return datosJsonString;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			datosJsonString = "Error Conexion";}
		return datosJsonString;

	}

}
